home *** CD-ROM | disk | FTP | other *** search
/ Total Web Page (Professional Suite) / Total Web Page 99.iso / CGI / FAKESSI.EXE < prev    next >
Text File  |  1996-06-10  |  7KB  |  417 lines

  1. #!/usr/bin/perl
  2.  
  3. # Server side include documentation at NCSA:
  4. #   <URL:http://hoohoo.ncsa.uiuc.edu/docs/tutorials/includes.html>
  5. # FakeSSI documentation:
  6. #   <URL:http://sw.cse.bris.ac.uk/WebTools/fakessi.html>
  7.  
  8. # These lines (in httpd.conf) make the whole thing work!!!
  9. # Change the directory to point at your own /cgi-bin/
  10. # Exec    /*.shtml      /users/www/cgi-bin/fakessi.pl
  11. # The line above is the PREFERRED usage. It only parses files with the 
  12. # extension of .shtml
  13.  
  14. # Alternatively, all files are parsed using the following two lines.
  15. # Exec    /*        /users/www/cgi-bin/fakessi.pl
  16. # Exec    /~*       /users/www/cgi-bin/fakessi.pl
  17.  
  18. # Configuration area.
  19. # $BINDIR is the /cgi-bin/ directory.
  20. $BINDIR='/users/ccnjb/cgi-bin';
  21.  
  22. # $DOCROOT is the Document root (easy really, innit??)
  23. $DOCROOT='/users';
  24.  
  25. # $INDEX is the name of the directory index file.
  26. $INDEX='index.html';
  27.  
  28. # $USER_DIR is the name of the user directory.
  29. $USER_DIR='public_html';
  30.  
  31. $errno=0;
  32.  
  33. # Set the default error message you want, the size format, time format and 
  34. # timezone here.
  35. $errmsg='[an error occurred while processing this directive]';
  36. $sizefmt='bytes';
  37. $timefmt="%A, %d-%b-%y %H:%M:%S %Z";
  38. $timezone='BST';
  39.  
  40. # OK, now to work!!!
  41. print("Content-type: text/html\n\n");
  42.  
  43. # Convert the target file name from WWW form into explicit form
  44. $sent=$ENV{SCRIPT_NAME};
  45. $ENV{'HTTP_REFERER'}=$sent;
  46. $infile=$sent;
  47. &MakePathname;
  48. $target=$outfile;
  49.  
  50. # Read in target WWW page, and make into one long line.
  51. open(FOO,$target);
  52. @lines = <FOO>;
  53. foreach $line (@lines) {
  54.     $bigline=join('',$bigline,$line);
  55. }
  56. close(FOO);
  57.  
  58. # Go thru the line until you reach the end looking for SSI's.
  59. $len=length($bigline);
  60. while ($len > 0)
  61. {
  62.  if ($bigline =~ /<!--#/ )
  63.  {
  64.   print($`);
  65.   if ($' =~ /-->/)
  66.   {
  67.    $ssi = $`;
  68.    $bigline = $';
  69.    &HandleSSI;
  70.    $len=length($bigline);
  71.   }
  72.  }
  73.  else
  74.  {
  75.   $len = 0;
  76.   print($bigline);
  77.  }
  78. }
  79.  
  80. sub HandleSSI {
  81.  if ($ssi =~ /config/i)
  82.  {
  83.   @var1=split('="',$ssi);
  84.   @var2=split('"',$var1[1]);
  85.   $var=$var2[0];
  86.   if ($ssi =~ /errmsg/i)
  87.   {
  88.    $errmsg=$var;
  89.   } 
  90.   elsif ($ssi =~ /sizefmt/i)
  91.   {
  92.    $sizefmt=$var;
  93.   }
  94.   elsif ($ssi =~ /timefmt/i)
  95.   {
  96.    $timefmt=$var;
  97.   }
  98.   else
  99.   {
  100.    &GiveErrMsg;
  101.   }
  102.  }
  103.  elsif ($ssi =~ /echo/i) 
  104.  {
  105.   @var1=split('var="',$ssi);
  106.   @var2=split('"',$var1[1]);
  107.   $var=$var2[0];
  108.   if ($var =~ /DOCUMENT_NAME/)
  109.   {
  110.    @out=split('/',substr($target,rindex($target,'/')));
  111.    print("$out[1]\n");
  112.   }
  113.   elsif ($var =~ /DOCUMENT_URI/)
  114.   {
  115.    @output=split($DOCROOT,$target);
  116.    print($output[1]);
  117.   }
  118.   elsif ($var =~ /DATE_GMT/)
  119.   {
  120.    $timetype = 0;
  121.    $nowtime=time();
  122.    &strftime;
  123.   }
  124.   elsif ($var =~ /DATE_LOCAL/)    
  125.   {
  126.    $timetype = 1;
  127.    $nowtime=time();
  128.    &strftime;
  129.   }
  130.   elsif ($var =~ /LAST_MODIFIED/)
  131.   {
  132.    $timetype = 1;
  133.    ($t,$t,$t,$t,$t,$t,$t,$t,$t,$nowtime,$t,$t,$t)=stat($target);
  134.    &strftime;
  135.   }
  136.   else
  137.   {
  138.    &GiveErrMsg;
  139.   }
  140.  }
  141.  elsif ($ssi =~ /exec/i)
  142.  {
  143.   if ($ssi =~ /cgi=/i)
  144.   {
  145.    @var1=split('cgi="',$ssi);
  146.    @var2=split('"',$var1[1]);
  147.    $var=$var2[0];
  148.    $infile=$var;
  149.    &MakePathname;
  150.    $var=$outfile;
  151.    if ($errno == 0)
  152.    {
  153.     open (FOO, "-|") || exec ($var); 
  154.     @pages = <FOO>;
  155.     foreach $page (@pages) {
  156.      print($page);
  157.     }
  158.    }
  159.   }
  160.  
  161. # Deliberate hold on CMD= tag ATM...
  162. #  elsif ($ssi =~ /cmd=/i)
  163.  
  164.  } 
  165.  elsif ($ssi =~ /include/i) 
  166.  {
  167.   &WhichFile;
  168.   if ($errno == 0)
  169.   {
  170.    open(FOO,$filename);
  171.    @lines = <FOO>;
  172.    foreach $line (@lines) {
  173.     print("$line");
  174.    }
  175.   }
  176.   else
  177.   {
  178.    &GiveErrMsg;
  179.   }
  180.  }
  181.  elsif ($ssi =~ /flastmod/i) 
  182.  {
  183.   &WhichFile;
  184.   if ($errno == 0)
  185.   {
  186.    ($t,$t,$t,$t,$t,$t,$t,$t,$t,$nowtime,$t,$t,$t)=stat($filename);
  187.    $timetype=1;
  188.    &strftime;
  189.   }
  190.   else
  191.   {
  192.    &GiveErrMsg;
  193.   }
  194.  }
  195.  elsif ($ssi =~ /fsize/i) 
  196.  {
  197.   &WhichFile;
  198.   if ($errno == 0)
  199.   {
  200.    ($t,$t,$t,$t,$t,$t,$t,$size,$t,$t,$t,$t,$t)=stat($filename);
  201.    if ($sizefmt =~ /abbrev/i)
  202.    {
  203.     print(int(($size / 1024) + 1),"Kbytes");
  204.    }
  205.    else
  206.    {
  207.     print("$size bytes");
  208.    }
  209.  
  210.   }
  211.  
  212.  }
  213.  else
  214.  {
  215.   &GiveErrMsg;
  216.  }
  217. }
  218.  
  219. sub MakePathname {
  220.  $errno=1;
  221.  $info=$infile;
  222.  if ($info =~ /^\/cgi-bin\//)
  223.  {
  224.   @split1=split(/\/cgi-bin\//,$info);
  225.   $info=join('/',$BINDIR,$split1[1]);
  226.  }
  227.  elsif ($info =~ /~/)
  228.  {
  229.   @split1=split(/~/,$info);
  230.   if ($split1[1] =~ /\//)
  231.   {
  232.    $user=$`;
  233.    ($t1,$t1,$t1,$t1,$t1,$t1,$t1,$dir,$t1)=getpwnam($user);
  234.    $dir=join('/',$dir,$USER_DIR);
  235.    $file=join('/',$dir,$');
  236.    $info=$file;
  237.   } 
  238.  } 
  239.  else
  240.  {
  241.   $new=join('',$DOCROOT,$info);
  242.   $info=$new;
  243.  }
  244.  
  245.  if ($info =~ /\/$/)
  246.  {
  247.   if (-d $info)
  248.   {
  249.    $file=join('',$info,$INDEX);
  250.    $info=$file;
  251.   } 
  252.  }
  253.  
  254.  $outfile=$info;
  255.  if (!-e $outfile)
  256.  {
  257.   &GiveErrMsg;
  258.  }
  259.  else
  260.  {
  261.   $errno=0;
  262.  }
  263. }
  264.  
  265. sub GiveErrMsg {
  266.  print("$errmsg");
  267. }
  268.  
  269. sub WhichFile {
  270.  $errno=1;
  271.  if ($ssi =~ /virtual="/i)
  272.  {
  273.   $temp=$';
  274.   $temp =~ /"/;
  275.   $filename = $`;
  276.  }
  277.  elsif ($ssi =~ /file="/i)
  278.  { 
  279.   $temp=$';
  280.   $temp =~ /"/;
  281.   $filename =join('/',substr($target,0,rindex($target,'/')),$`);
  282.  }
  283.  if (-e $filename)
  284.  {
  285.   $errno = 0;
  286.  }
  287. }
  288.  
  289. sub strftime {
  290. # Setup day and month names for later use.
  291.  @sday=('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
  292.  @lday=('Monday','Tuesday','Wednesday',
  293.         'Thursday','Friday','Saturday','Sunday');
  294.  @smon=('Jan','Feb','Mar','Apr','May','Jun',
  295.        'Jul','Aug','Sep','Oct','Nov','Dec');
  296.  @lmon=('January','February','March','April','May','June',
  297.        'July','August','September','October','November','December');
  298.  
  299. # Get time units
  300.  if ($timetype == 0)
  301.  {
  302.   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime($nowtime);
  303.  }
  304.  else
  305.  {
  306.   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($nowtime);
  307.  }
  308.  
  309. # Print time
  310.  $timestr=$timefmt;
  311.  $len=length($timestr);
  312.  
  313.  while ($len > 0)
  314.  {
  315.  
  316.   $extra='';
  317.  
  318.   if ($timestr =~/^%a/)
  319.   {
  320.    print("$sday[$day]");
  321.   }
  322.   elsif ($timestr =~/^%A/)
  323.   {
  324.    print("$lday[$day]");
  325.   }
  326.   elsif ($timestr =~/^%b/)
  327.   {
  328.    print("$smon[$mon]");
  329.   }
  330.   elsif ($timestr =~/^%B/)
  331.   {
  332.   print("$lmon[$mon]");
  333.   }
  334.   elsif ($timestr =~/^%d/)
  335.   {
  336.    print("$mday");
  337.   }
  338.   elsif ($timestr =~/^%H/) 
  339.   {
  340.    print("$hour");
  341.   }   
  342.   elsif ($timestr =~/^%I/) 
  343.   {
  344.    $thour=$hour;
  345.    if ($thour > 12)
  346.    {
  347.     $thour = $thour - 12;
  348.    }
  349.    print("$thour");
  350.   }   
  351.   elsif ($timestr =~/^%j/) 
  352.   {
  353.    print("$yday");
  354.   }   
  355.   elsif ($timestr =~/^%m/) 
  356.   {
  357.    print("$mon");
  358.   }   
  359.   elsif ($timestr =~/^%M/) 
  360.   {
  361.    print("$min");
  362.   }   
  363.   elsif ($timestr =~/^%p/) 
  364.   {
  365.    if ($hour > 11)
  366.    {
  367.     print("pm");
  368.    }
  369.    else
  370.    {
  371.     print("am");
  372.    }
  373.   }   
  374.   elsif ($timestr =~/^%S/) 
  375.   {
  376.    print("$sec");
  377.   }   
  378.   elsif ($timestr =~/^%w/) 
  379.   {
  380.    print("$wday");
  381.   }   
  382.   elsif ($timestr =~/^%y/)
  383.   {
  384.    print("$year");
  385.   }
  386.   elsif ($timestr =~/^%Z/)
  387.   {
  388.    print("$timezone");
  389.   }
  390.   elsif ($timestr =~/^%%/) 
  391.   {
  392.    print("%");
  393.   }   
  394.   else
  395.   {
  396.    if ($timestr =~ /^%./)
  397.    {
  398.     print("<b>$+</b>");
  399.    }
  400.    elsif ($timestr =~ /%/)
  401.    {
  402.     print("$`");
  403.     $extra=join('','%',$');
  404.    }
  405.   }
  406.   if (length($extra) > 0)
  407.   {
  408.    $timestr=$extra;
  409.   }
  410.   else
  411.   {
  412.    $timestr=$';
  413.   }
  414.   $len=length($timestr);
  415.  }
  416. }
  417.